<template>
  <div class="user-look">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录帐户设置">
          <el-form
            class="form"
            label-width="100px"
            :model="form"
            :rules="rules"
            ref="form"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input show-password v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
              <el-button @click="cancelClick">取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" lazy>
          <!-- 有公司有硬性要求：每个vue文件代码不能达到多少行  800行 -->
          <Info
            :userInfo="propUserInfo"
            @updateUserInfo="updateUserInfo"
          ></Info>
        </el-tab-pane>
        <el-tab-pane label="岗位信息" lazy>
          <Job></Job>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser, sysUserPut } from '@/api/user'
// import Info from './components/Info.vue'
export default {
  components: {
    //   异步组件,按需使用
    Info: () => import('./components/Info.vue'),
    Job: () => import('./components/Job.vue')
  },
  data () {
    return {
      userId: this.$route.params.id,
      form: {
        username: '',
        password: ''
      },
      userInfo: {},
      propUserInfo: {}, // 用于传递给Info组件使用的
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '6-12位密码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysUser(this.userId)
      // 只需要回显用户名就可以了
      this.userInfo = res.data
      //   中断info组件内的个人详情与当前组件的关联
      this.propUserInfo = JSON.parse(JSON.stringify(res.data))
      this.form.username = res.data.username
      console.log('当前的用户信息', res)
    },
    // 更新点击
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          // 得到完整 用户信息，且要拿到修改后的数据
          // 合并  userInfo 与form
          // 合并对象  {...this.userInfo,...this.form}  Object.assign({},this.userInfo,this.form)
          await sysUserPut({ ...this.userInfo, ...this.form })
          //   父组件内修改了，子组件内的数据也要同步更新
          this.propUserInfo.username = this.form.username
          this.propUserInfo.password = this.form.password
          // 将用户信息同步到个人详情显示
          this.$message.success('修改用户信息成功')
        }
        console.log(result)
      })
    },
    // 取消点击事件
    cancelClick () {
      //   this.$router.go(-1)
      this.$router.push('/employees')
    },
    // 定义一个方法用于子组件内触发更新
    updateUserInfo (obj) {
      this.userInfo = JSON.parse(JSON.stringify(obj))
      this.form.username = obj.username
    }
  }
}
</script>
<style lang="scss" scoped>
.user-look {
  padding: 15px;
  .form {
    padding-top: 50px;
    width: 600px;
    margin: 0 auto;
  }
}
</style>
